/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package za.co.pas.business.model;

import java.util.LinkedList;
import java.util.List;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import za.co.pas.business.abstracts.AbstractDataModel;
import za.co.pas.business.data.UserGroups;
import za.co.pas.business.data.UserType;
import za.co.pas.business.message.StatusMessage;
import za.co.pas.business.message.StatusMessageObject;

/**
 *
 * @author alabuschagne
 */
public class ConfDataModel extends AbstractDataModel
{
    private static Logger logger = Logger.getLogger(ConfDataModel.class);
    private List<UserGroups> userGroups = new LinkedList<UserGroups>();
    private List<UserType> userTypes = new LinkedList<UserType>();

    /**
     * @return the costingTypes
     */
    public List<UserGroups> getUserGroups(boolean force)
    {
        if ((force) || (userGroups == null) || (userGroups.isEmpty()))
        {
            userGroups = getUserGroupsDB();
        }
        return userGroups;
    }

    /**
     * @param costingTypes the costingTypes to set
     */
    public void setUserGroups(List<UserGroups> userGroups)
    {
        this.userGroups = userGroups;
    }

    /**
     * @return the roomTypes
     */
    public List<UserType> getUserTypes(boolean force)
    {
        if ((force) || (userTypes == null) || (userTypes.isEmpty()))
        {
            userTypes = getUserTypesDB();
        }
        return userTypes;
    }

    /**
     * @param roomTypes the roomTypes to set
     */
    public void setUserTypes(List<UserType> userTypes)
    {
        this.userTypes = userTypes;
    }

    private List<UserType> getUserTypesDB()
    {
        try
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, "Getting all user types", StatusMessageObject.SMO_TYPE_READ));
            Query q = em.createQuery("SELECT r FROM UserType r");
            setConnectionOk(true);
            return (List<UserType>) q.getResultList();
        }
        catch (NoResultException ex)
        {
            //This is not a error
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_WARN, "No results for user types", StatusMessageObject.SMO_TYPE_READ));
            logger.info(ex.getMessage());
        }
        catch (NonUniqueResultException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalStateException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalArgumentException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }



    /**
     * @return the bedTypes
     */
    public List<UserGroups> getUserGroupsDB()
    {
        try
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, "Getting all User Groups", StatusMessageObject.SMO_TYPE_READ));
            Query q = em.createQuery("SELECT r FROM UserGroups r");
            setConnectionOk(true);
            return (List<UserGroups>) q.getResultList();
        }
        catch (NoResultException ex)
        {
            //This is not a error
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_WARN, "No results for User Groups", StatusMessageObject.SMO_TYPE_READ));
            logger.info(ex.getMessage());
        }
        catch (NonUniqueResultException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalStateException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalArgumentException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }

    public void add(UserGroups ug)
    {
        if(userGroups == null)
        {
           userGroups = new LinkedList<UserGroups>();
        }
        userGroups.add(ug);
    }

    public void add(UserType ut)
    {
        if(userTypes == null)
        {
           userTypes = new LinkedList<UserType>();
        }
        userTypes.add(ut);
    }

//    public void add(CostingType ct)
//    {
//        if(costingTypes == null)
//        {
//            costingTypes = new LinkedList<CostingType>();
//        }
//        costingTypes.add(ct);
//    }

}
